package br.com.lucasddaniel.merchanmobile.activities;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import br.com.lucasddaniel.merchanmobile.Controller;
import br.com.lucasddaniel.merchanmobile.R;
import br.com.lucasddaniel.merchanmobile.dao.ExcessaoBanco;

public class MenuActivity extends Activity implements OnClickListener {

	private final String ERRO = "Erro";
	private final String ALERTA = "Alerta";
	private final Handler mHandler = new Handler();

	private ImageView imgTuristas;
	private ImageView imgPacotes;
	// private ImageView imgImportarDados;
	// private ImageView imgExportarDados;
	private ImageView imgCadastro;
	private ImageView imgConfigRelatorio;
	private boolean showMenu;

	@Override
	public void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_menu );
		Intent i = getIntent();
		if( i.getExtras() != null ) {
			showMenu = i.getBooleanExtra( "showMenu", false );
		}
		initializeComponents();
	}

	private void initializeComponents() {
		// imgExportarDados = ( ImageView ) findViewById( R.id_activity_menu.img_exportardados );
		// imgImportarDados = ( ImageView ) findViewById( R.id_activity_menu.img_importardados );
		imgPacotes = ( ImageView ) findViewById( R.id_activity_menu.img_pacotes );
		imgTuristas = ( ImageView ) findViewById( R.id_activity_menu.img_turistas );
		imgCadastro = ( ImageView ) findViewById( R.id_activity_menu.img_cadastro );
		// imgImprimir = ( ImageView ) findViewById( R.id_activity_menu.img_imprimir );
		imgConfigRelatorio = ( ImageView ) findViewById( R.id_activity_menu.img_conf_relat );

		// imgExportarDados.setOnClickListener( this );
		// imgImportarDados.setOnClickListener( this );
		imgPacotes.setOnClickListener( this );
		imgTuristas.setOnClickListener( this );

		imgCadastro.setOnClickListener( this );
		imgConfigRelatorio.setOnClickListener( this );
	}

	public void onClick( View view ) {
		switch( view.getId() ) {
			case R.id_activity_menu.img_exportardados:
				try {
					Controller.getInstance().exportarDados( getApplicationContext() );

					Toast.makeText( this, getResources().getString( R.string.activity_menu_toast_exportar ), Toast.LENGTH_LONG ).show();
				} catch( FileNotFoundException e ) {
					notificarUsuario( ALERTA, e.getMessage() );
				} catch( IOException e ) {
					notificarUsuario( ERRO, e.getMessage() );
				} catch( ExcessaoBanco e ) {
					notificarUsuario( ERRO, e.getMessage() );
				}
				break;
			case R.id_activity_menu.img_turistas:
				startActivity( new Intent( this, ListarTuristasActivity.class ) );
				break;
			case R.id_activity_menu.img_pacotes:
				startActivity( new Intent( this, ListarPacotesActivity.class ) );
				break;
			case R.id_activity_menu.img_importardados:
				boolean sucesso = false;
				try {
					sucesso = Controller.getInstance().importarDados( getApplicationContext() );
				} catch( FileNotFoundException e ) {
					notificarUsuario( ALERTA, e.getMessage() );
				} catch( IOException e ) {
					notificarUsuario( ERRO, e.getMessage() );
				} catch( ExcessaoBanco e ) {
					notificarUsuario( ERRO, e.getMessage() );
				} finally {
					if( sucesso ) {
						AlertDialog.Builder alertDialog = new AlertDialog.Builder( this );
						alertDialog.setTitle( "Dados importados com sucesso" );
						alertDialog.setCancelable( true );
						alertDialog.setNeutralButton( "OK", new DialogInterface.OnClickListener() {
							public void onClick( DialogInterface arg0, int arg1 ) {
							}
						} );
						alertDialog.show();
					}
				}
				break;
			case R.id_activity_menu.img_cadastro:
				Intent i = new Intent( this, TuristasActivity.class );
				i.putExtra( "fromMenu", true );
				startActivity( i );
				break;
		}

	}

	/**
	 * Exibe uma mensagem ao usuario de acordo com os parametros passados. Usada em excessoes.
	 * 
	 * @param titulo
	 * @param message
	 */
	protected void notificarUsuario( final String titulo, final String message ) {
		mHandler.post( new Runnable() {
			public void run() {
				new AlertDialog.Builder( MenuActivity.this ).setTitle( titulo ).setMessage( message ).create().show();
			}
		} );
	}

	@Override
	public boolean onCreateOptionsMenu( Menu menu ) {
		if( showMenu ) {
			menu.add( Menu.NONE, Menu.FIRST, Menu.NONE, getResources().getString( R.string.menu_label_custom ) );
		}
		return super.onCreateOptionsMenu( menu );
	}

	@Override
	public boolean onOptionsItemSelected( MenuItem item ) {
		if( item.getItemId() == Menu.FIRST ) {
			Intent intent = new Intent( this, CadastroResponsavelActivity.class );
			this.startActivity( intent );
		}

		return super.onOptionsItemSelected( item );
	}
}
